package com.android.sq.zouqi.main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.R;
import com.android.sq.zouqi.event.pCtrlEventInfo;
import com.android.sq.zouqi.event.pTableEventInfo;
import com.android.sq.zouqi.relationship.pCtrlRelation;
import com.android.sq.zouqi.relationship.pTableRelation;
import com.android.sq.zouqi.user.pTableUserInfo;

public class mainActivity extends Activity {
	private Context context;
	private TextView tv;
	private Button btn;
	/*
	 * 需要测试那个控制类就实例化那个控制类
	 */
	private pCtrlEventInfo ctrl_event;
	private pCtrlRelation ctrl_relation;
	
	private MyThread myThread;
	// ctrlEventInfo
	private int get_signal = 1;
	private int save_signal = 2;
	private int send_signal = 3;
	private int statechange_signal = 4;
	private int geteventlist_signal = 5;
	private int confirminvitation_signal = 6;
	private int eventunconfirmed_signal = 7;
	// ctrlRelation
	private int send_relation_request_signal = 8;
	private int confirm_relation_signal = 9;
	private int send_relation_requests_signal = 10;
	private int get_relation_list_signal = 11;
	
	public pTableEventInfo event;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == get_signal) {
				showResult1();
			} else if (msg.what == save_signal) {
				Bundle data = msg.getData();
				int result_id = data.getInt("RESULT_ID");
				showResult2("bbbbbb",result_id);
			} else if (msg.what == send_signal) {
				Bundle data = msg.getData();
				int result_multi_query = data.getInt("RESULT_MULTI_QUERY");
				showResult3("multi_query = ", "" + result_multi_query);
			} else if (msg.what == statechange_signal) {
				Bundle data = msg.getData();
				int result_id = data.getInt("RESULT_STATE_CHANGE");
				showResult2("aaaaa\n", result_id);
			} else if (msg.what == geteventlist_signal) {
				Bundle data = msg.getData();
				String result_str = data.getString("RESULT_GET_EVENT_LIST");
				showResult3("get_event_list, size = ", result_str);
			} else if (msg.what == confirminvitation_signal) {
				Bundle data = msg.getData();
				int result_id = data.getInt("RESULT_CONFIRM_INVITATION");
				showResult2("Result_confirm_invitation\n被确认的记录id是:", result_id);
			} else if (msg.what == eventunconfirmed_signal) {
				Bundle data = msg.getData();
				String result_str = data.getString("RESULT_EVENT_UNCONFIRMED");
				showResult3("get_event_unconfirmed\n", result_str);
			} else if (msg.what == send_relation_request_signal) {
				Bundle data = msg.getData();
				int result_int = data.getInt("RESULT_SEND_RELATION_REQUEST");
				showResult2("Result_send_relation_request:\n返回的记录ID是：", result_int);
			} else if (msg.what == confirm_relation_signal) {
				Bundle data = msg.getData();
				int result_int = data.getInt("RESULT_CONFIRM_RELATION");
				showResult2("Result_confirm_relation\n返回的记录ID是：", result_int);
			} else if (msg.what == send_relation_requests_signal) {
				Bundle data = msg.getData();
				int result_int = data.getInt("RESULT_SEND_RELATION_REQUESTS");
				Log.e("BA", "handler, result_int = " + result_int);
				showResult2("Result_send_relation_requestS:\n返回的记录ID是(1表示全部操作成功，0表示不成功)：", result_int);
			} else if (msg.what == get_relation_list_signal) {
				Bundle data = msg.getData();
				String result_str = data.getString("RESULT_GET_RELATION_LIST");
				showResult3("get_relation_list\n", result_str);
			}
		}
	};
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		Log.e("BA", "In mainActivity");
		
		tv = (TextView) findViewById(R.id.main_showevent_tv);
		btn = (Button) findViewById(R.id.main_showevent_btn);
		
		context = this.getApplicationContext();
		ctrl_event = new pCtrlEventInfo(context, handler);
		ctrl_relation = new pCtrlRelation(context, handler);
		
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				myThread = new MyThread();
				myThread.start();
			}
		});
	}
	
	public void showResult1() {
		int _id = event.get_id();
		int state = event.getpEventState();
		String start_time = event.getpEventStart();
		String duration = event.getpEventDuration();
		String byname = event.getpEventByname();
		int owner_id = event.getpEventOwnerId();
		tv.setText(_id + ":"+ state + " " + byname + " " + start_time + " " + duration + " " + owner_id);
	}
	
	public void showResult2(String title, int result_id) {
		tv.setText(title + "return id = " + result_id + ", TIME = " + System.currentTimeMillis());
	}
	
	public void showResult3(String title, String result_multi_query) {
		tv.setText(title + result_multi_query);
	}
	
	/*
	 * 以下都是测试方法，在mainActivity中
	 */
	public void getEvent() {
		event = ctrl_event.pGetEventInfo(1);
		handler.sendEmptyMessage(get_signal);
	}
	
	public void saveEvent() {
		event = new pTableEventInfo();
		event.set_id(1);
		event.setpEventState(Constants.EVENT_STATE_UNSTARTED);
		long current_time = System.currentTimeMillis();
		String cur_time_str = String.valueOf(current_time);
		Log.e("BA", "CURRENT_TIME = " + cur_time_str);
		event.setpEventStart(cur_time_str.trim());
		event.setpEventDuration(cur_time_str.trim());
			
		//event.setpEventStart("2012-09-09 19:19:19");
		//event.setpEventDuration("2012-06-12 09:11:12");
		//event.setpEventLocationId(1);
		event.setpEventOwnerId(642);
		int result_id = ctrl_event.pSaveEventInfo(event);
			
		Message msg = new Message();
		msg.what = save_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_ID", result_id);
		handler.sendMessage(msg);
	}
	
	/*public void sendInvitation() {
		List<pTableUserInfo> users = new ArrayList<pTableUserInfo>();
		users.add(new pTableUserInfo(648));
		users.add(new pTableUserInfo(646));
		users.add(new pTableUserInfo(650));
		int result = ctrl_event.pSendInvitation(18, users);
		Message msg = new Message();
		msg.what = send_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_MULTI_QUERY", result);
		handler.sendMessage(msg);
	}*/
	
	public void stateChange() {
		// 修改id为x的活动的状态为y
		int result = ctrl_event.pEventStateChange(18, Constants.EVENT_STATE_ROLLING);
		Message msg = new Message();
		msg.what = statechange_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_STATE_CHANGE", result);
		handler.sendMessage(msg);
	}
	
	public void getEventList() {
		int user_id = 647;
		int event_state = Constants.EVENT_STATE_UNSTARTED;
		
		List<pTableEventInfo> events = ctrl_event.pGetEventList(user_id, event_state);
		String result = "去掉注释即可";
		
		for (int i = 0; i < events.size(); i++) {
			pTableEventInfo event = events.get(i);
			result += i + ":" + event.get_id() + "-" + event.getpEventState() + "-" 
					+ event.getpEventStart() + "-" + event.getpEventDuration() + "-"
				 	+ "-" + event.getpEventOwnerId() + "\n";
		}
		Message msg = new Message();
		msg.what = geteventlist_signal;
		Bundle data = msg.getData();
		data.putString("RESULT_GET_EVENT_LIST", result);
		handler.sendMessage(msg);
	}
	
	public void confirmInvitation() {
		int user_id = 648;
		int event_id = 18;
		
		int result = ctrl_event.pConfirmInvitation(user_id, event_id);
		Message msg = new Message();
		msg.what = confirminvitation_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_CONFIRM_INVITATION", result);
		handler.sendMessage(msg);
	}
	
	public void getEventUnconfirmed() {
		int user_id = 642;
		List<pTableEventInfo> events = ctrl_event.pGetEventUnconfirmed(user_id);
		String result = "";
		/*int size = events.length;
		for (int i = 0; i < size; i++) {
			result += i + ":" + events[i].get_id() + "-" + events[i].getpEventState() + "-" 
					+ events[i].getpEventStart() + "-" + events[i].getpEventDuration() + "-"
					+ events[i].getpEventLocationId() + "-" + events[i].getpEventOwnerId() + "\n";
		}*/
		Message msg = new Message();
		msg.what = eventunconfirmed_signal;
		Bundle data = msg.getData();
		data.putString("RESULT_EVENT_UNCONFIRMED", result);
		handler.sendMessage(msg);
	}
	
	/*------------------以下是测试pCtrlRelation------------------*/
	public void sendRelationRequest() {
		int user_id = 642;
		int friend_id = 646;
		int result = ctrl_relation.pSendRelationRequest(user_id, friend_id);
		
		Message msg = new Message();
		msg.what = send_relation_request_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_SEND_RELATION_REQUEST", result);
		handler.sendMessage(msg);
	}
	
	public void confirmRelation() {
		int relation_id = 3360;
		int pUserId = 647;
		int pFriendId = 653;
		int pRelationState = 1;
		pTableRelation relation = new pTableRelation();
		relation.set_id(relation_id);
		relation.setpUserId(pUserId);
		relation.setpFriendId(pFriendId);
		relation.setpRelationState(pRelationState);
		int result = ctrl_relation.pConfirmRelation(relation);
		
		Message msg = new Message();
		msg.what = confirm_relation_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_CONFIRM_RELATION", result);
		handler.sendMessage(msg);
	}
	
	public void sendRelationRequests() {
		// 也就是用户642想他的好友648 649 659 651发出好友关系确认请求
		int user_id = 642;
		int friends_id[] = {648, 649, 650, 651};
		
		int result = ctrl_relation.pSendRelationRequests(user_id, friends_id);
		Log.e("BA", "CTRL, result = " + result);
		Message msg = new Message();
		msg.what = send_relation_requests_signal;
		Bundle data = msg.getData();
		data.putInt("RESULT_SEND_RELATION_REQUESTS", result);
		handler.sendMessage(msg);
	}
	
	public void getRelationList() {
		int user_id = 647;
		int relation_state = Constants.RELATION_BOTH_CONFIRM;
		LinkedList<pTableRelation> relations = ctrl_relation.pGetRelationList(user_id, relation_state);
		int size = relations.size();
		String result_str = "records count = " + size + "\n";
		int i = 1;
		for (Iterator<pTableRelation> it = relations.iterator(); it.hasNext();) {
			pTableRelation relation = it.next();
			result_str += (i++) + ": " + relation.get_id() + " " + relation.getpUserId() + " "
					+ relation.getpFriendId() + " " + relation.getpRelationState() + "\n";
		}
		Message msg = new Message();
		msg.what = get_relation_list_signal;
		Bundle data = msg.getData();
		data.putString("RESULT_GET_RELATION_LIST", result_str);
		handler.sendMessage(msg);
	}
	
	class MyThread extends Thread {
		
		public void run() {
			getEventList();
		}
	}
}
